package com.an.groupecho;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.GlobalEventExecutor;

public class MyHandler extends SimpleChannelInboundHandler<String>{
	
	// 客户端消息统计
	private AtomicInteger count = new AtomicInteger();
	
	
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
	
		// 数据读取完毕
		ctx.writeAndFlush(Unpooled.copiedBuffer("hellooo--->>>>" + count.getAndIncrement(),CharsetUtil.UTF_8));
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		// 异常就关闭tongdao
		ctx.close();
	}

	// 管理所有的channel
	private static ChannelGroup channelGroup  = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
	private SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.sss");
	

	// 一旦链接被建立第一个执行
	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
	
		System.out.println("链接建立：：：" + ctx.channel().remoteAddress());
		// 将该客户加入聊天的信息推送给其他在线的客户段
		// channelGroup会遍历所有的channel执行消息的发送
		channelGroup.writeAndFlush("[客户端，" + ctx.channel().remoteAddress() + "]上线了，当前时间【" + sdf.format(new Date()) + "】\n");
		channelGroup.add(ctx.channel());
		System.err.println("handlerAdded,当前客户端数量：" + channelGroup.size());
	}
	
	// 断开链接，提示客户离线
	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
//		channelGroup.remove(ctx.channel());
		channelGroup.writeAndFlush("[客户端，" + ctx.channel().remoteAddress() + "]离线了，当前时间【" + sdf.format(new Date()) + "】\n");
		System.err.println("handlerRemoved,当前客户端数量：" + channelGroup.size());
	}
	
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println(ctx.channel().remoteAddress() + "上线了·==~~~");
	}
	
	// 表示channel处于不活动状态，提示XX离线了。
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
	
		System.out.println(ctx.channel().remoteAddress() + "离线了·==~~~");

	}
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {

		System.out.println("服务器读取线程：" + Thread.currentThread().getName());
		System.out.println("server ctx :" + ctx);
		System.out.println("channel 和 pipline的关系：");
		Channel channel = ctx.channel();
		ChannelPipeline pipeline = ctx.pipeline();
		ChannelPipeline pipeline2 = channel.pipeline();
		System.out.println("ctx.pipeline():" + pipeline);
		System.out.println("ctx.channel().pipeline():" + pipeline2);
		System.out.println("客户端发送的消息：" + msg);
		System.out.println("客户端地址：" + channel.remoteAddress());
		
		// 获取到当前的channel
		channelGroup.forEach(ch ->{
			if(ch != channel) {
				ch.writeAndFlush("[客户]"+ channel.remoteAddress() + "说：" + msg + "\n");
			}
		});
		
	}
	
}
